package resoft.jeasyform.components;

import charvax.swing.JPasswordField;
import resoft.jeasyform.event.*;

/**
 * <p>password filed</p>
 * User: albert
 * Date: Aug 27, 2007
 * Time: 12:13:52 AM
 */
public class PasswordField extends InputField {

    public String getValue() {
        if(jPasswordField!=null) {
            this.value = new String(jPasswordField.getPassword());
        }
        return value;
    }

    public void setValue(String value) {
        if(value==null) {
            value = "";
        }
        this.value = value;
    }

    /**
     * to swing object
     *
     * @return charva.awt.Component
     */
    public charva.awt.Component getSwingObject() {
        jPasswordField = new JPasswordField(value);
        jPasswordField.setBounds(getY(),getX(),getY() + getHeight(),getX() + getWidth());
        jPasswordField.setPaddingChar('_');
        jPasswordField.setColumns(getWidth());

        jPasswordField.addKeyListener(new FixedLengthEventHandler(this,getMaxLength()));
        jPasswordField.addActionListener(new InputFieldEnterPressedEventAdapter(getParentWindow(),this,getPostEventChain()));

        return jPasswordField;
    }

    /**
     * hide
     */
    public void hide() {
        jPasswordField.setVisible(false);
        setAcceptFocus(false);
    }

    /**
     * show
     */
    public void show() {
        jPasswordField.setVisible(true);
        setAcceptFocus(true);
    }


    /**
     * get focus
     */
    public void requestFocus() {
        jPasswordField.requestFocus();
        getPreEvent().execute(getParentWindow(),this, EventType.TextField_PreEvent);
    }

    private String value = "";
    private JPasswordField jPasswordField;

}
